import { save, info, getAutoKey } from '@/api/oa/index.js'
export default {
  data () {
    return {
    }
  },
  created () {
    console.log('minxin')
  },
  methods: {
    getData () {
      if (this.uploadArr.length > 0) {
        this.uploadArr.forEach(item => {
          this.file_paths.push(item.path)
          this.file_names.push(item.name)
        })
      }
      const params = {
        // token: window.localStorage.getItem('token'),
        openid: window.localStorage.getItem('token'),
        reason: this.reason,
        department_id: this.department,
        truename: this.truename,
        apply_date: this.applyTime,
        approve_id: this.approve.id
      }
      // if (this.file_paths.length > 0) {
      //   const paths = new Set(this.file_paths)
      //   const names = new Set(this.file_names)
      //   params.file_paths = Array.from(paths).join()
      //   params.file_names = Array.from(names).join()
      // }
      const paths = new Set(this.file_paths)
      const names = new Set(this.file_names)
      params.file_paths = Array.from(paths).join()
      params.file_names = Array.from(names).join()
      this.addDofieldRender()
      if (this.field_ids.length > 0) {
        const paths = new Set(this.field_ids)
        const names = this.field_vals
        params.field_ids = Array.from(paths).join()
        params.field_vals = Array.from(names).join()
      }
      if (!this.reason) {
        this.$message.error('请输入申请原因')
        return false
      }
      if (!this.applyTime) {
        this.$message.error('请输入考核周期')
        return false
      }
      if (!this.truename) {
        this.$message.error('请输入姓名')
        return false
      }
      if (!this.gangwei) {
        this.$message.error('请输入职位')
        return false
      }
      save(params).then((res) => {
        const { status } = res
        if (status) {
          this.$message.error('提交成功')
          this.goFrom()
        }
      })
      this.$store.commit('getUpload', [])
    },
    submitFunc () {
      if (!this.flag) {
        this.flag = true
        this.getData()
        setTimeout(() => {
          this.flag = false
        }, 5000)
      }
    },
    infoFunc () {
      const params = {
        // token: window.localStorage.getItem('token'),
        openid: window.localStorage.getItem('token'),
        approve_id: this.approve.approve_id,
        workflow_id: this.approve.workflow_id
      }
      info(params).then(res => {
        const { status, data: { workflow }, data: { fields }, data: { node } } = res
        if (status) {
          this.reason = workflow.reason
          this.contents = workflow.contents
          this.value = workflow.truename
          this.department = workflow.department_id
          window.localStorage.setItem('approve_node_id', workflow.approve_node_id)
          window.localStorage.setItem('is_end', workflow.is_end)
          if (fields && fields.length > 0) {
            this.fieldsInfo(fields)
          }
          if (node && node.length > 0) {
            this.nodeInfo(node)
          }
          this.getAutoKey(workflow.approve_node_id)
        }
      })
    },
    //  info 中fields字段
    fieldsInfo (fields) {
      fields && fields.forEach(e => {
        if (e.field_name === '考核周期') {
          this.applyTime = e.field_val
        }
        // if (e.field_name === '部门') {
        //   this.department = Number(e.field_val)
        // }
        if (e.field_name === '姓名') {
          this.truename = e.field_val
        }
        if (e.field_name === '职位' || e.field_name === '岗位') {
          this.gangwei = e.field_val
        }
        if (this.obj[`a${e.approve_field_id}`] !== undefined) {
          this.$set(e, 'val', this.obj[`a${e.approve_field_id}`])
          this.obj[`a${e.approve_field_id}`] = e.field_val
          this.obj[`a${e.approve_field_id}Flag`] = true
        }
        if (this.obj[`a${e.field_id}`] !== undefined) {
          this.$set(e, 'val', this.obj[`a${e.field_id}`])
          this.obj[`a${e.field_id}`] = e.field_val
          this.obj[`a${e.field_id}Flag`] = true
        }
      })
    },
    nodeInfo (node) {
      // const name = []
      node && node.forEach(e => {
        e.fields && e.fields.forEach(j => {
          for (let i = 0; i < e.fields.length; i++) {
            if (this.obj[`a${j.field_id}`] !== undefined) {
              this.obj[`a${j.field_id}`] = j.field_val
              this.obj[`a${j.field_id}Flag`] = true
            }
          }
          return j
        })
        return e
      })
    },
    renderCustom () {
      this.autoField && this.autoField.forEach(e => {
        this.judge(e)
        if (e.val) {
          this.field_ids.push(e.approve_field_id)
          this.field_vals.push(e.val)
        }
        return e
      })
      console.log(this.field_ids, this.field_vals)
    },
    // 获取自定义字段
    getAutoKey (id) {
      getAutoKey(id).then(res => {
        const { status, data } = res
        this.autoField = data
        if (status) {
          this.autoField && this.autoField.forEach(e => {
            this.judge(e)
            return e
          })
        }
      })
    },
    //  选择申请人
    selectStaff (e) {
      this.truename = e.truename
      this.department = e.department_id
    },
    addDofieldRender () {
      this.addDofield && this.addDofield.forEach(e => {
        this.judge(e)
        if (e.val) {
          this.field_ids.push(e.field_id)
          this.field_vals.push(e.val)
        }
        return e
      })
      console.log('add', this.addDofield)
    },
    judge (e) {
      if (e.field_name === '考核周期') {
        this.$set(e, 'val', this.applyTime)
      }
      if (e.field_name === '部门') {
        this.$set(e, 'val', this.department)
      }
      if (e.field_name === '姓名') {
        this.$set(e, 'val', this.truename)
      }
      if (e.field_name === '职位' || e.field_name === '岗位') {
        if (!this.gangwei) {
          this.gangwei = e.options
        }
        // this.gangwei = e.options
        this.$set(e, 'val', this.gangwei)
      }
      if (this.obj[`a${e.approve_field_id}`] !== undefined && e.approve_field_id) {
        this.obj[`a${e.approve_field_id}`] = e.options
        this.$set(e, 'val', this.obj[`a${e.approve_field_id}`])
        this.obj[`a${e.approve_field_id}Flag`] = false
      }
      if (!this.obj[`a${e.field_id}`]) {
        this.obj[`a${e.field_id}`] = e.options
      }
      if (this.obj[`a${e.field_id}`] !== undefined && e.field_id) {
        this.$set(e, 'val', this.obj[`a${e.field_id}`])
        this.obj[`a${e.field_id}Flag`] = false
        if (e.field_name === '职位' || e.field_name === '岗位') {
          if (!this.gangwei) {
            this.gangwei = e.options
          }
          this.$set(e, 'val', this.gangwei)
        }
      }
    },
    goFrom () {
      const num = this.$route.query.active
      this.$router.push({ name: 'oaList', query: { activeName: num } })
    },
    autoName () {
      if (this.writInput.field_name === '人力资源意见') {
        this.renliname = this.userInfo.truename
      }
      if (this.writInput.field_name === '财务部意见') {
        this.caiwuname = this.userInfo.truename
      }
      if (this.writInput.field_name === '总经理意见') {
        this.jingliname = this.userInfo.truename
      }
    }
  }
}
